package com.tanhua.server.interceptor;

import com.tanhua.model.domain.User;

/**
 * @Author Pan Rundong
 * @Date 2023-02-25 19:12
 * @Desc 用户线程处理
 */

public class UserThreadLocal {
    private static ThreadLocal<User> threadLocal = new ThreadLocal<>();

    /**
     * 设置用户信息进入线程
     * @Author: Pan Rundong
     * @Date: 2023-02-25 19:15
     * @Greet2u:  （O｀ 3′O）~~♡
     * @param user
     * @return void
     */
    public static void setThreadLocal(User user) {
        threadLocal.set(user);
    }

    /**
     * 从线程获取用户信息
     * @Author: Pan Rundong
     * @Date: 2023-02-25 19:16
     * @Greet2u:  （O｀ 3′O）~~♡
     * @return com.tanhua.model.domain.User
     */
    public static User getThreadLocal() {
        return threadLocal.get();
    }

    /**
     * 从线程获取用户Id
     * @Author: Pan Rundong
     * @Date: 2023-02-25 19:17
     * @Greet2u:  （O｀ 3′O）~~♡
     * @return java.lang.Long
     */
    public static Long getUserId() {
        if (threadLocal.get() == null) {
            return null;
        }
        return threadLocal.get().getId();
    }

    /**
     * 从线程获取用户手机
     * @Author: Pan Rundong
     * @Date: 2023-02-25 19:20
     * @Greet2u:  （O｀ 3′O）~~♡
     * @return java.lang.String
     */
    public static String getPhone() {
        if (threadLocal.get() == null) {
            return null;
        }
        return threadLocal.get().getMobile();
    }

    /**
     * 移除线程信息
     * @Author: Pan Rundong
     * @Date: 2023-02-25 19:20
     * @Greet2u:  （O｀ 3′O）~~♡
     * @return void
     */
    public static void remove() {
        threadLocal.remove();
    }
}
